<?php
class ImageLib{
	static $ftp_image_connect_id=false;
	
	static function ftp_image_connect(){
		if(!ImageLib::$ftp_image_connect_id){
			ImageLib::$ftp_image_connect_id = ftp_connect(FTP_IMAGE_SERVER);

			if(ImageLib::$ftp_image_connect_id){
				// Login to FTP Server
				$login_result= ftp_login(ImageLib::$ftp_image_connect_id, FTP_IMAGE_USER, FTP_IMAGE_PASSWORD);
				if($login_result){
					// turn passive mode on
					ftp_pasv(ImageLib::$ftp_image_connect_id, true);
					return true;
				}
				return false;
			}
			return false;
		}
		return true;
	}
	
	static function ftp_image_close(){
		if(ImageLib::$ftp_image_connect_id){
			ftp_close(ImageLib::$ftp_image_connect_id);
			ImageLib::$ftp_image_connect_id=false;
		}
	}

	static function ftp_image_put_file($destFileName,$sourceFileName,$mode=FTP_BINARY){
		if(self::ftp_image_connect()){
			if(@ftp_put(ImageLib::$ftp_image_connect_id, $destFileName, $sourceFileName, $mode)){
				@ftp_chmod ( ImageLib::$ftp_image_connect_id, 0777, $destFileName );
				return true;
			}
		}
		return false;
	}
	
	static function ftp_image_delete_file($sourceFileName){
		if(self::ftp_image_connect()){
			if(@ftp_delete(ImageLib::$ftp_image_connect_id, $sourceFileName)){
				return true;
			}
		}
		return false;
	}

	static  function ftp_check_dir($remote_dir_path,$mkdir=true){
		$ret = true;
		if(self::ftp_image_connect()){
			if($remote_dir_path=='')
			return true;

			$dir=split("/", $remote_dir_path);
			$remote_dir_path="";

			for ($i=0;$i<count($dir);$i++){
				if($dir[$i]!=''){
					$remote_dir_path.="/".$dir[$i];
					if(!@ftp_chdir(ImageLib::$ftp_image_connect_id,$remote_dir_path)){
						if($mkdir){
							@ftp_chdir(ImageLib::$ftp_image_connect_id,"/");

							if(!@ftp_mkdir(ImageLib::$ftp_image_connect_id,$remote_dir_path)){
								$ret=false;
								break;
							}
						}
						else{
							$ret=false;
							break;
						}
					}
				}
			}
			@ftp_chdir(ImageLib::$ftp_image_connect_id,"/");
		}
		else{
			$ret = false;
		}
		return $ret;
	}
	
	static function CheckDir($pDir){
        if (is_dir($pDir))return true;
        if (!@mkdir($pDir,0777,true)){return false;}
        return true;
    }
    
    static function empty_all_dir($name, $remove = false){
		if(is_dir($name)){
			if($dir = opendir($name)){
				$dirs = array();
				while($file=readdir($dir)){
					if($file!='..' and $file!='.'){
						if(is_dir($name.'/'.$file)){
							$dirs[]=$file;
						}
						else{
							@unlink($name.'/'.$file);
						}
					}
				}
				closedir($dir);
				foreach($dirs as $dir_){
					self::empty_all_dir($name.'/'.$dir_, $remove);
				}
				
				if($remove){
					@rmdir($name);
				}
			}		
		}
	}
}
?>
